#include"Matrix.h"


Matrix::Matrix()
{
	Identity();
}

Matrix::~Matrix()
{
}


Matrix Matrix::operator+(const Matrix& rmat) const
{
	Matrix ma;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			ma.mat[i][j]=this->mat[i][j]+rmat.mat[i][j];
		}
	}
	return ma;
}

Matrix Matrix::operator-(const Matrix& rmat) const
{
	Matrix ma;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			ma.mat[i][j] = this->mat[i][j] - rmat.mat[i][j];
		}
	}
	return ma;
}

Matrix Matrix::operator*(const Matrix& rmat) const
{
	Matrix ma;
	for (int i = 0; i < 4; i++) {		
		for (int j = 0; j < 4; j++) {
			ma.mat[i][j] = 0;
			for (int k = 0; k < 4; k++) {
				ma.mat[i][j] += this->mat[i][k] * rmat.mat[k][j];
			}
			
		}
	}
	return ma;
}

Matrix Matrix::operator*(float t) const
{
	Matrix ma;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			ma.mat[i][j] = this->mat[i][j]* t;
		}
	}
	return ma;
}



Vec4f Matrix::MultiplyVec4(const Vec4f& v) const 
{
	float x, y, z, w;
	x = mat[0][0] * v.x + mat[0][1] * v.y + mat[0][2] * v.z + mat[0][3] * v.w;
	y = mat[1][0] * v.x + mat[1][1] * v.y + mat[1][2] * v.z + mat[1][3] * v.w;
	z = mat[2][0] * v.x + mat[2][1] * v.y + mat[2][2] * v.z + mat[2][3] * v.w;
	w = mat[3][0] * v.x + mat[3][1] * v.y + mat[3][2] * v.z + mat[3][3] * v.w;
	return Vec4f(x, y, z, w);

}

void Matrix::Identity()
{
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (i == j) mat[i][j] = 1;
			else {
				mat[i][j] = 0;
			}
		}
	}
}

Matrix Matrix::Transpose() const
{
	Matrix trans;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			trans.mat[i][j] = mat[j][i];
		}
	}
	return trans;
}

void Matrix::Print()
{
	std::cout << "Matrix:" << std::endl;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			std::cout << "(" << mat[i][j] << ")";
		}
		std::cout << std::endl;
	}
}
